<template>
  <div class="icon-body">
    <el-input v-model="name" style="position: relative;" clearable placeholder="请输入图标名称" @clear="filterIcons" @input.native="filterIcons">
      <i slot="suffix" class="el-icon-search el-input__icon" />
    </el-input>
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="Icons" name="icon">
        <div class="grid">
          <div v-for="item of iconList" :key="item" @click="selectedIcon(item)">
            <div class="icon-item">
              <svg-icon :icon-class="item" class-name="disabled" />
              <span>{{ item }}</span>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="Element-UI Icons" name="elIcon">
        <div class="grid">
          <div v-for="item of elIconList" :key="item" @click="selectedIcon(item)">
            <div class="icon-item">
              <i :class="item" />
              <span>{{ item }}</span>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import icons from './requireIcons'
import elIcons from './element-icons'

export default {
  name: 'IconSelect',
  data() {
    return {
      activeName: 'icon',
      name: '',
      iconList: icons,
      elIconList: elIcons
    }
  },
  methods: {
    filterIcons() {
      this.iconList = icons
      this.elIconList = elIcons
      if (this.activeName === 'icon') {
        if (this.name) {
          this.iconList = this.iconList.filter(item => item.includes(this.name))
        }
      }
      if (this.activeName === 'elIcon') {
        if (this.name) {
          this.elIconList = this.elIconList.filter(item => item.includes(this.name))
        }
      }
    },
    selectedIcon(name) {
      this.$emit('selected', name)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = icons
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .icon-body {
    width: 100%;
    // padding: 5px;

    .grid {
      height: 300px;
      overflow-y: scroll;
      //position: relative;
      //display: grid;
      //grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));

      .icon-item {
        margin: 5px;
        height: 80px;
        text-align: center;
        width: 80px;
        float: left;
        font-size: 30px;
        color: #24292e;
        cursor: pointer;
      }
      span {
        display: block;
        font-size: 10px;
        margin-top: 5px;
      }
    }

    .disabled {
      pointer-events: none;
    }
  }
</style>
